<?php
declare (strict_types = 1);

namespace app\admin\model\erp;

use bbweb\traits\ModelTrait;
use bbweb\basic\BaseModel;

/**
 * 固定资产模型
 */
class ErpVoucher extends BaseModel
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'erp_voucher';

    use ModelTrait;

    // 设置字段信息
    protected $schema = [
        'id'            => 'int',
        'period'        => 'int',//凭证期间
        'period_code'   => 'int',//凭证编号
        'code'          => 'varchar',//科目编码
        'code_name'     => 'varchar',//科目描述
        'debit'         => 'double',//借方
        'credit'        => 'double',//贷方
        'position'      => 'tinyint',//排序
        'period_at'     => 'int',//凭证日期
        'create_at'     => 'int',//
        'update_at'     => 'int'
    ];

    public static function delOrder($period_code, $order_sn) {
        return false;
    }

    /*
     * 获取本科目及所有父科目数据
     */
    public static function getPush($sid, $data = []) {
        if(!$sid) return $data;
        $info = ErpSubjects::where('id',$sid)->field('id,pid,code,name,pname,level')->find();
        if(!$info) return $data;
        $info['debit'] = round(self::where('period','=','202100')->where('code','like',$info['code'].'%')->sum('debit'),2);
        $info['credit'] = round(self::where('period','=','202100')->where('code','like',$info['code'].'%')->sum('credit'),2);
        $data[] = $info;
        if($info['pid']) {
            $data = self::getPush($info['pid'], $data);
        }
        return $data;
    }
}